﻿using System;
using System.Drawing;
using Company.BLL.DuLieuChuan;
using Company.BLL.GC;
using Company.BLL.KDT;
using Company.Interface.SXXK;
using Janus.Windows.GridEX.EditControls;
using Company.Interface.GC;
using Company.BLL.Utils;

namespace Company.Interface
{
    public partial class HangMauDichEditForm : BaseForm
    {
        public bool IsEdited = false;
        public bool IsDeleted = false;
        //-----------------------------------------------------------------------------------------
        public HangMauDich HMD = new HangMauDich();
        public HangMauDichCollection collection=new HangMauDichCollection();
        public HopDong HD = new HopDong();
        public string LoaiHangHoa = "";
        public string NhomLoaiHinh = string.Empty;
        
        public decimal TyGiaTT;
        public string MaNguyenTe;
        public string MaNuocXX;
        
        //-----------------------------------------------------------------------------------------				
        // Tính thuế
        private decimal luong;
        private decimal dgnt;
        private decimal tgnt;

        private decimal tgtt_nk;
        private decimal tgtt_ttdb;
        private decimal tgtt_gtgt;
        private decimal clg;

        private decimal ts_nk;
        private decimal ts_ttdb;
        private decimal ts_gtgt;
        private decimal tl_clg;

        private decimal tt_nk;
        private decimal tt_ttdb;
        private decimal tt_gtgt;
        private decimal st_clg;
        //-----------------------------------------------------------------------------------------
        private string MaHangOld = "";
        public HangMauDichEditForm()
        {
            InitializeComponent();
        }

        private void khoitao_GiaoDien()
        {
            if (this.NhomLoaiHinh.Substring(1, 2) == "GC" || this.NhomLoaiHinh.Substring(1, 2) == "SX")
            {
                txtMaHang.ButtonStyle = EditButtonStyle.Ellipsis;
                txtTenHang.ReadOnly = cbDonViTinh.ReadOnly = true;
                txtMaHang.BackColor = txtTenHang.BackColor = cbDonViTinh.BackColor = Color.FromArgb(255, 255, 192);
            }
            
        }
        
        private void khoitao_DuLieuChuan()
        {
            // Nước XX.
            this._Nuoc = Nuoc.SelectAll();

            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();
            cbDonViTinh.DataSource = this._DonViTinh;
            cbDonViTinh.SelectedValue = GlobalSettings.DVT_MAC_DINH;
        }
        
        private decimal tinhthue()
        {
            this.tgnt = this.dgnt * this.luong;
            this.tgtt_nk = this.tgnt * this.TyGiaTT;
            this.tt_nk = this.tgtt_nk * this.ts_nk;

            this.tgtt_ttdb = this.tgtt_nk + this.tt_nk;
            this.tt_ttdb = this.tgtt_ttdb * ts_ttdb;

            this.tgtt_gtgt = this.tgtt_ttdb + this.tt_ttdb;
            this.tt_gtgt = this.tgtt_gtgt * this.ts_gtgt;

            this.clg = this.tgtt_nk;
            this.st_clg = this.clg * this.tl_clg;

            txtTGNT.Value = this.tgnt;
            txtTGTT_NK.Value = this.tgtt_nk;
            txtTGTT_TTDB.Value = this.tgtt_ttdb;
            txtTGTT_GTGT.Value = this.tgtt_gtgt;
            txtCLG.Value = this.clg;

            txtTienThue_NK.Value = this.tt_nk;
            txtTienThue_TTDB.Value = this.tt_ttdb;
            txtTienThue_GTGT.Value = this.tt_gtgt;
            txtTien_CLG.Value = this.st_clg;
            
            txtTongSoTienThue.Value = this.tt_nk + this.tt_ttdb + this.tt_gtgt + this.st_clg;
            
            return this.tt_nk + this.tt_ttdb + this.tt_gtgt + this.st_clg;
        }

        //-----------------------------------------------------------------------------------------

        private void HangMauDichEditForm_Load(object sender, EventArgs e)
        {
            ctrNuocXX.Ma = this.HMD.NuocXX_ID;
            this.khoitao_DuLieuChuan();
            this.khoitao_GiaoDien();
            lblNguyenTe_DGNT.Text = lblNguyenTe_TGNT.Text = "(" + this.MaNguyenTe + ")";
            toolTip1.SetToolTip(lblNguyenTe_DGNT, this.MaNguyenTe);
            toolTip1.SetToolTip(lblNguyenTe_TGNT, this.MaNguyenTe);

            // Tính lại thuế.
            //this.HMD.TinhThue(this.TyGiaTT);
            lblTyGiaTT.Text = "Tỷ giá VNĐ: " + this.TyGiaTT.ToString("N");
            
            // Bind data.
            txtMaHang.Text = this.HMD.MaPhu;
            txtTenHang.Text = this.HMD.TenHang;
            txtMaHS.Text = this.HMD.MaHS;
            cbDonViTinh.SelectedValue = this.HMD.DVT_ID;
            
            //luu lai ma hang
            MaHangOld = this.HMD.MaPhu;
            
            txtDGNT.Value = this.dgnt = this.HMD.DonGiaKB;
            txtLuong.Value = this.luong = this.HMD.SoLuong;
            txtTGNT.Value = this.HMD.TriGiaKB;
            txtTS_NK.Value = this.ts_nk = this.HMD.ThueSuatXNK;
            txtTS_TTDB.Value = this.ts_ttdb = this.HMD.ThueSuatTTDB;
            txtTS_GTGT.Value = this.ts_gtgt = this.HMD.ThueSuatGTGT;
            txtTL_CLG.Value = this.tl_clg = this.HMD.TyLeThuKhac;

            this.ts_nk = this.HMD.ThueSuatXNK / 100;
            this.ts_ttdb = this.HMD.ThueSuatTTDB / 100;
            this.ts_gtgt = this.HMD.ThueSuatGTGT / 100;
            this.tl_clg = this.HMD.TyLeThuKhac / 100;            
            
            this.tinhthue();
        }

        private void txtLuong_Leave(object sender, EventArgs e)
        {
            this.luong = Convert.ToDecimal(txtLuong.Value);
            this.tinhthue();
        }

        private void txtDGNT_Leave(object sender, EventArgs e)
        {
            this.dgnt = Convert.ToDecimal(txtDGNT.Value);
            this.tinhthue();
        }

        private void txtTS_NK_Leave(object sender, EventArgs e)
        {
            this.ts_nk = Convert.ToDecimal(txtTS_NK.Value) / 100;
            this.tinhthue();
        }

        private void txtTS_TTDB_Leave(object sender, EventArgs e)
        {
            this.ts_ttdb = Convert.ToDecimal(txtTS_TTDB.Value) / 100;
            this.tinhthue();
        }

        private void txtTS_GTGT_Leave(object sender, EventArgs e)
        {
            this.ts_gtgt = Convert.ToDecimal(txtTS_GTGT.Value) / 100;
            this.tinhthue();
        }

        private void txtTS_CLG_Leave(object sender, EventArgs e)
        {
            this.tl_clg = Convert.ToDecimal(txtTL_CLG.Value) / 100;
            this.tinhthue();
        }

        //-----------------------------------------------------------------------------------------------

        private void chkMienThue_CheckedChanged(object sender, EventArgs e)
        {
            grbThue.Enabled = !chkMienThue.Checked;
            txtTS_NK.Value = 0;
            txtTS_TTDB.Value = 0;
            txtTS_GTGT.Value = 0;
            txtTL_CLG.Value = 0;
            this.tinhthue();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaHang_ButtonClick(object sender, EventArgs e)
        {
            switch (this.NhomLoaiHinh)
            {
                case "NSX":
                    NguyenPhuLieuRegistedForm f = new NguyenPhuLieuRegistedForm();
                    f.CalledForm = "HangMauDichForm";
                    f.MaHaiQuan = this.MaHaiQuan;
                    f.ShowDialog();
                    if (f.NguyenPhuLieuSelected.Ma != "")
                    {
                        txtMaHang.Text = f.NguyenPhuLieuSelected.Ma;
                        txtTenHang.Text = f.NguyenPhuLieuSelected.Ten;
                        txtMaHS.Text = f.NguyenPhuLieuSelected.MaHS;
                        cbDonViTinh.SelectedValue = f.NguyenPhuLieuSelected.DVT_ID.PadRight(3);
                    }
                    break;
                case "XSX":
                    if (this.LoaiHangHoa == "N")
                    {
                        NguyenPhuLieuRegistedForm fNPLX = new NguyenPhuLieuRegistedForm();
                        fNPLX.CalledForm = "HangMauDichForm";
                        fNPLX.MaHaiQuan = this.MaHaiQuan;
                        fNPLX.ShowDialog();
                        if (fNPLX.NguyenPhuLieuSelected.Ma != "")
                        {
                            txtMaHang.Text = fNPLX.NguyenPhuLieuSelected.Ma;
                            txtTenHang.Text = fNPLX.NguyenPhuLieuSelected.Ten;
                            txtMaHS.Text = fNPLX.NguyenPhuLieuSelected.MaHS;
                            cbDonViTinh.SelectedValue = fNPLX.NguyenPhuLieuSelected.DVT_ID.PadRight(3);
                        }
                    }
                    else
                    {
                        SanPhamRegistedForm fSPX = new SanPhamRegistedForm();
                        fSPX.CalledForm = "HangMauDichForm";
                        fSPX.MaHaiQuan = this.MaHaiQuan;
                        fSPX.ShowDialog();
                        if (fSPX.SanPhamSelected.Ma != "")
                        {
                            txtMaHang.Text = fSPX.SanPhamSelected.Ma;
                            txtTenHang.Text = fSPX.SanPhamSelected.Ten;
                            txtMaHS.Text = fSPX.SanPhamSelected.MaHS;
                            cbDonViTinh.SelectedValue = fSPX.SanPhamSelected.DVT_ID.PadRight(3);
                        }
                    }
                    break;
                case "NGC":
                    if (this.LoaiHangHoa == "N")
                    {
                        NguyenPhuLieu npl = new NguyenPhuLieu();
                        npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        npl.MaHaiQuan = HD.MaHaiQuan;      
                        
                        npl.SoHopDong = HD.SoHopDong;
                        npl.NgayKy = HD.NgayKy;
                        NguyenPhuLieuRegistedGCForm f1 = new NguyenPhuLieuRegistedGCForm();
                        f1.NguyenPhuLieuSelected = npl;
                        f1.ShowDialog();
                        if (f1.NguyenPhuLieuSelected.Ma != "")
                        {
                            txtMaHang.Text = npl.Ma;
                            txtTenHang.Text = npl.Ten;
                            txtMaHS.Text = npl.MaHS;
                            cbDonViTinh.SelectedValue = npl.DVT_ID;
                        }
                    }
                    else
                    {
                        ThietBi tb = new ThietBi();
                        tb.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        tb.MaHaiQuan = HD.MaHaiQuan;
                        tb.NgayKy = HD.NgayKy;
                        tb.SoHopDong = HD.SoHopDong;
                        ThietBiRegistedForm ftb = new ThietBiRegistedForm();
                        ftb.ThietBiSelected = tb;
                        ftb.ShowDialog();
                        if (ftb.ThietBiSelected.Ma != "")
                        {
                            txtMaHang.Text = tb.Ma;
                            txtTenHang.Text = tb.Ten;
                            txtMaHS.Text = tb.MaHS;
                            cbDonViTinh.SelectedValue = tb.DVT_ID;
                        }
                    }
                    break;
                case "XGC":
                    if (this.LoaiHangHoa == "N")
                    {
                        NguyenPhuLieu npl = new NguyenPhuLieu();
                        npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        npl.MaHaiQuan = HD.MaHaiQuan;
                        npl.NgayKy = HD.NgayKy;
                        npl.SoHopDong = HD.SoHopDong;
                        NguyenPhuLieuRegistedGCForm f2 = new NguyenPhuLieuRegistedGCForm();
                        f2.NguyenPhuLieuSelected = npl;
                        f2.ShowDialog();
                        if (f2.NguyenPhuLieuSelected.Ma != "")
                        {
                            txtMaHang.Text = npl.Ma;
                            txtTenHang.Text = npl.Ten;
                            txtMaHS.Text = npl.MaHS;
                            cbDonViTinh.SelectedValue = npl.DVT_ID;
                        }
                    }
                    else if (LoaiHangHoa == "S")
                    {
                        SanPham sp = new SanPham();
                        sp.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        sp.MaHaiQuan = HD.MaHaiQuan;
                        sp.NgayKy = HD.NgayKy;
                        sp.SoHopDong = HD.SoHopDong;
                        SanPhamRegistedGCForm f3 = new SanPhamRegistedGCForm();
                        f3.SanPhamSelected = sp;
                        f3.ShowDialog();
                        if (f3.SanPhamSelected.Ma != "")
                        {
                            txtMaHang.Text = sp.Ma;
                            txtTenHang.Text = sp.Ten;
                            txtMaHS.Text = sp.MaHS;
                            cbDonViTinh.SelectedValue = sp.DVT_ID;
                        }
                    }
                    else
                    {
                        ThietBi tb = new ThietBi();
                        tb.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        tb.MaHaiQuan = HD.MaHaiQuan;
                        tb.NgayKy = HD.NgayKy;
                        tb.SoHopDong = HD.SoHopDong;
                        ThietBiRegistedForm ftb = new ThietBiRegistedForm();
                        ftb.ThietBiSelected = tb;
                        ftb.ShowDialog();
                        if (ftb.ThietBiSelected.Ma != "")
                        {
                            txtMaHang.Text = tb.Ma;
                            txtTenHang.Text = tb.Ten;
                            txtMaHS.Text = tb.MaHS;
                            cbDonViTinh.SelectedValue = tb.DVT_ID;
                        }
                    }
                    break;
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            cvError.Validate();
            if (!cvError.IsValid) return;
            if (!MaHS.Validate(txtMaHS.Text, 10))
            {
                epError.SetIconPadding(txtMaHS, -8);
                epError.SetError(txtMaHS, "Mã số HS không hợp lệ.");
                return;
            }
            if (checkMaHangExit(txtMaHang.Text.Trim()))
            {
                ShowMessage("Đã có hàng này trong danh sách.", false);
                return;
            }
            this.HMD.MaHS = txtMaHS.Text;
            this.HMD.MaPhu = txtMaHang.Text;
            this.HMD.TenHang = txtTenHang.Text;
            this.HMD.NuocXX_ID = ctrNuocXX.Ma;
            this.HMD.DVT_ID = cbDonViTinh.SelectedValue.ToString();
            this.HMD.SoLuong = Convert.ToDecimal(txtLuong.Text);
            this.HMD.DonGiaKB = Convert.ToDecimal(txtDGNT.Text);
            this.HMD.TriGiaKB = Convert.ToDecimal(txtTGNT.Text);
            this.HMD.TriGiaTT = Convert.ToDecimal(txtTGTT_NK.Text);
            this.HMD.ThueSuatXNK = Convert.ToDecimal(txtTS_NK.Text);
            this.HMD.ThueSuatTTDB = Convert.ToDecimal(txtTS_TTDB.Text);
            this.HMD.ThueSuatGTGT = Convert.ToDecimal(txtTS_GTGT.Text);
            this.HMD.ThueXNK = Convert.ToDecimal(txtTienThue_NK.Text);
            this.HMD.ThueTTDB = Convert.ToDecimal(txtTienThue_TTDB.Text);
            this.HMD.ThueGTGT = Convert.ToDecimal(txtTienThue_GTGT.Text);
            if (chkMienThue.Checked)
                this.HMD.MienThue = 1;
            else
                this.HMD.MienThue = 0;

            // Tính thuế.
            this.HMD.TinhThue(this.TyGiaTT);
            this.IsEdited = true;
            txtMaHang.Text = "";
            txtTenHang.Text = "";
            txtMaHS.Text = "";
            txtLuong.Value = 0;
            txtDGNT.Value = 0;
            this.Close();
        }
        private bool checkMaHangExit(string maHang)
        {
            foreach (HangMauDich hmd in collection)
            {
                if (hmd.MaPhu.Trim() == maHang && maHang!=MaHangOld) return true;               
            }
            return false;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.IsDeleted = true;
            this.Close();
        }
     
        private void txtMaHang_Leave(object sender, EventArgs e)
        {
            if (LoaiHangHoa == "N")
            {
                Company.BLL.SXXK.NguyenPhuLieu npl = new Company.BLL.SXXK.NguyenPhuLieu();
                npl.Ma = txtMaHang.Text.Trim();
                npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                npl.MaHaiQuan = MaHaiQuan;
                if (npl.Load())
                {
                    txtMaHang.Text = npl.Ma;
                    txtMaHS.Text = npl.MaHS;
                    txtTenHang.Text = npl.Ten;
                    cbDonViTinh.SelectedValue = npl.DVT_ID;
                }
                else
                {
                    epError.SetIconPadding(txtMaHang, -8);
                    epError.SetError(txtMaHang, "Không tồn tại nguyên phụ liệu này.");
                     
                    txtTenHang.Text = txtMaHS.Text = string.Empty;
                    cbDonViTinh.SelectedValue = "";
                }
            }
            else
            {
                Company.BLL.SXXK.SanPham sp = new Company.BLL.SXXK.SanPham();
                sp.Ma = txtMaHang.Text.Trim();
                sp.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                sp.MaHaiQuan = MaHaiQuan;
                if (sp.Load())
                {
                    txtMaHang.Text = sp.Ma;
                    txtMaHS.Text = sp.MaHS;
                    txtTenHang.Text = sp.Ten;
                    cbDonViTinh.SelectedValue = sp.DVT_ID;
                }
                else
                {
                    epError.SetIconPadding(txtMaHang, -8);
                    epError.SetError(txtMaHang, "Không tồn tại sản phẩm này.");
                    txtTenHang.Text = txtMaHS.Text = string.Empty;
                    cbDonViTinh.SelectedValue = "";
                }
            }
        }

    }
}
